package com.dayaokeji.rhythmschoolstudent.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.StateListDrawable;
import android.support.annotation.NonNull;

import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.mine.data.AppThemes;


/**
 * Created by Gorden on 2018/3/15.
 */

public class ThemeUtil {

    /**
     * 得到对于颜色的图片
     * @param context
     * @param resId
     * @return
     */
    public static BitmapDrawable changeDrawableColor(Context context, Integer resId) {
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resId);
        return getBitmapDrawable(context, bitmap, getThemeColor());
    }

    /**
     * 改变图片颜色
     * @param context
     * @param bitmap
     * @param colorResId
     * @return
     */
    @NonNull
    private static BitmapDrawable getBitmapDrawable(Context context, Bitmap bitmap, Integer colorResId) {
        int color = context.getResources().getColor(colorResId);
        Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
        final Canvas canvas = new Canvas(newBitmap);
        final Paint paint = new Paint();
        float[] src = new float[]{
                0, 0, 0, 0, Color.red(color),
                0, 0, 0, 0, Color.green(color),
                0, 0, 0, 0, Color.blue(color),
                0, 0, 0, 1, 0};
        final ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.set(src);
        paint.setAntiAlias(true);
        paint.setColorFilter(new ColorMatrixColorFilter(src));
        canvas.drawBitmap(bitmap, new Matrix(), paint);
        return new BitmapDrawable(context.getResources(), newBitmap);
    }

    /**
     * 主题对于的全局颜色
     * @return
     */
    public static Integer getThemeColor() {
        switch (PrefUtils.getChooseThemeType()) {
            case AppThemes.DEFAULT_THEME:
                return R.color.colorPrimary;
            case AppThemes.TECHNOLOGY_BLUE_THEME:
                return R.color.technology_blue;
            case AppThemes.YOUTH_GREEN_THEME:
                return R.color.youth_green;
            default:
                return R.color.colorPrimary;
        }
    }

    /**
     * SateDrawable List change drawable color
     *
     * @param context
     * @param normalResId
     * @param sateResId
     * @return
     */
    public static StateListDrawable getSateListDrawable(Context context, @NonNull Integer normalResId, @NonNull Integer sateResId) {
        final StateListDrawable stateListDrawable = new StateListDrawable();
        stateListDrawable.addState(new int[]{android.R.attr.state_checked}, changeDrawableColor(context, sateResId));
        stateListDrawable.addState(new int[]{}, context.getResources().getDrawable(normalResId));
        return stateListDrawable;
    }


}
